package com.snow.springboot.controller;

import com.alibaba.fastjson.JSONObject;
import com.snow.springboot.pojo.domain.DiagnoseInfo;
import com.snow.springboot.pojo.domain.PageBean;
import com.snow.springboot.service.DiagnoseService;
import com.snow.springboot.task.ConsumeDiagnoseTask;
import com.snow.springboot.task.ProductDiagnoseTask;
import com.snow.springboot.task.SaveDiagnoseInfoTask;
import com.snow.springboot.util.MultiTaskPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Package: com.snow.springboot.controller
 * @ClassName: DiagnoseController
 * @Description: 诊断信息操作controller
 * @author: lurui
 * @Date: 2018/10/30 2:47 PM
 * @version:
 */
@RestController
@RequestMapping("/diagnoses")
public class DiagnoseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(DiagnoseController.class);
    /**
     * 并发线程数
     */
    public static final int THREAD_COUNT = 30;
    /**
     * 页码数
     */
    public static final int PAGE = 1;
    /**
     * 分页SIZE
     */
    public static final int SIZE = 10;

    @Value("${diagnosepath}")
    private String filePath;

    @Autowired
    private DiagnoseService diagnoseService;

    @GetMapping("/batch-add")
    public JSONObject batchAddDiagnose() {
        BlockingQueue<DiagnoseInfo> queue = new LinkedBlockingQueue<DiagnoseInfo>(100);
        MultiTaskPool multiTaskPool = MultiTaskPool.getInstance();
        List<DiagnoseInfo> result = diagnoseService.getDiagnosesByExcel(filePath);
        JSONObject rspData = new JSONObject();
        multiTaskPool.executeJob(new ProductDiagnoseTask(result, queue));
        for (int i = 0; i < THREAD_COUNT; i++) {
            multiTaskPool.executeJob(new SaveDiagnoseInfoTask(diagnoseService, queue));
        }
        rspData.put("rspCode", "SUCCESS");
        return rspData;
    }


    @GetMapping(value = "/batch-update")
    @ResponseBody
    public JSONObject updateDiagnoseInfo() {
        BlockingQueue<DiagnoseInfo> queue = new LinkedBlockingQueue<DiagnoseInfo>(100);
        MultiTaskPool multiTaskPool = MultiTaskPool.getInstance();
        PageBean pageBean = diagnoseService.getDiagnoseList(PAGE, SIZE);
        long total = pageBean.getTotal();
        List<DiagnoseInfo> diagnoseInfos = pageBean.getList();
        multiTaskPool.executeJob(new ProductDiagnoseTask(diagnoseInfos, queue));
        for (int i = 0; i < THREAD_COUNT; i++) {
            multiTaskPool.executeJob(new ConsumeDiagnoseTask(diagnoseService, queue));
        }
        JSONObject result = new JSONObject();
        result.put("result", "OK");
        return result;
    }

}
